﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Com.OpenCode.Framework.Core;
using DevExpress.Utils;
using DevExpress.XtraBars.Ribbon;

namespace Com.OpenCode.Framework.DevForm
{
    /// <summary>
    ///  Ribbon主菜单界面
    /// </summary>
    public class RibbonUI : RibbonControl, IMenuVisible
    {
        private static RibbonUI _Isnstanse;
        private Timer _Timer = new Timer();

        public RibbonUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            Plugin = args.Plugin;
            _Timer.Tick += _timer_Tick;
            _Timer.Interval = 1000;
            _Timer.Start();
            _Isnstanse = this;
            ShowToolbarCustomizeItem = false;
            ShowApplicationButton = DefaultBoolean.False;
            Dock = DockStyle.Top;
            RibbonStyle = RibbonControlStyle.Office2013;
            (args.Owner as MainForm).Controls.Add(this);
        }

        public Plugin Plugin { get; private set; }

        public static RibbonUI Instanse
        {
            get { return _Isnstanse; }
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            Refresh();
        }

        /// <summary>
        ///     根据当前菜单计算字体长度。
        /// </summary>
        /// <param name="text"></param>
        /// <param name="px"></param>
        /// <returns></returns>
        public string SplitCaption(string text, double px)
        {
            Graphics graphics = CreateGraphics();
            string str1 = "";
            string str2 = "";
            foreach (char t in text)
            {
                str1 += t;
                SizeF size = graphics.MeasureString(str1, Font);
                if (size.Width > px)
                {
                    str2 += "\r\n" + t;
                    str1 = "";
                }
                else
                {
                    str2 += t;
                }
            }
            return str2;
        }

        public void CheckVisible()
        {
            for (int i = 0; i < Pages.Count; i++)
            {
                (Pages[i] as IMenuVisible).CheckVisible();
            }
        }
    }
}